Completed
Push — master ( e0634c...a8a1a1 )
by Roy
02:22
created

stripe-admin.js ➔ jQuery   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 108

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 108
rs 8.2857
c 0
b 0
f 0
cc 1
nc 1
nop 1

1 Function

Rating   Name   Duplication   Size   Complexity  
B stripe-admin.js ➔ ... ➔ wc_stripe_admin.init 0 94 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
jQuery( function( $ ) {
2
	'use strict';
3
4
	/**
5
	 * Object to handle Stripe admin functions.
6
	 */
7
	var wc_stripe_admin = {
8
		/**
9
		 * Initialize.
10
		 */
11
		init: function() {
12
			$( document.body ).on( 'change', '#woocommerce_stripe_testmode', function() {
13
				var test_secret_key = $( '#woocommerce_stripe_test_secret_key' ).parents( 'tr' ).eq( 0 ),
14
					test_publishable_key = $( '#woocommerce_stripe_test_publishable_key' ).parents( 'tr' ).eq( 0 ),
15
					live_secret_key = $( '#woocommerce_stripe_secret_key' ).parents( 'tr' ).eq( 0 ),
16
					live_publishable_key = $( '#woocommerce_stripe_publishable_key' ).parents( 'tr' ).eq( 0 );
17
18
				if ( $( this ).is( ':checked' ) ) {
19
					test_secret_key.show();
20
					test_publishable_key.show();
21
					live_secret_key.hide();
22
					live_publishable_key.hide();
23
				} else {
24
					test_secret_key.hide();
25
					test_publishable_key.hide();
26
					live_secret_key.show();
27
					live_publishable_key.show();
28
				}
29
			} );
30
31
			$( '#woocommerce_stripe_testmode' ).change();
32
33
			$( '#woocommerce_stripe_stripe_checkout' ).change( function() {
34
				if ( $( this ).is( ':checked' ) ) {
35
					$( '#woocommerce_stripe_stripe_checkout_locale, #woocommerce_stripe_stripe_bitcoin, #woocommerce_stripe_stripe_checkout_image, #woocommerce_stripe_allow_remember_me' ).closest( 'tr' ).show();
36
					$( '#woocommerce_stripe_request_payment_api' ).closest( 'tr' ).hide();
37
				} else {
38
					$( '#woocommerce_stripe_stripe_checkout_locale, #woocommerce_stripe_stripe_bitcoin, #woocommerce_stripe_stripe_checkout_image, #woocommerce_stripe_allow_remember_me' ).closest( 'tr' ).hide();
39
					$( '#woocommerce_stripe_request_payment_api' ).closest( 'tr' ).show();
40
				}
41
			}).change();
42
43
			$( '#woocommerce_stripe_apple_pay' ).change( function() {
44
				if ( $( this ).is( ':checked' ) ) {
45
					$( '#woocommerce_stripe_apple_pay_button, #woocommerce_stripe_apple_pay_button_lang, #wc-gateway-stripe-apple-pay-domain' ).closest( 'tr' ).show();
46
				} else {
47
					$( '#woocommerce_stripe_apple_pay_button, #woocommerce_stripe_apple_pay_button_lang, #wc-gateway-stripe-apple-pay-domain' ).closest( 'tr' ).hide();
48
				}
49
			}).change();
50
51
			$( '#woocommerce_stripe_secret_key, #woocommerce_stripe_publishable_key' ).change( function() {
52
				var value = $( this ).val();
53
54
				if ( value.indexOf( '_test_' ) >= 0 ) {
55
					$( this ).css( 'border-color', 'red' ).after( '<span class="description stripe-error-description" style="color:red; display:block;">' + wc_stripe_admin_params.localized_messages.not_valid_live_key_msg + '</span>' );
0 ignored issues
show
Bug introduced by
The variable wc_stripe_admin_params seems to be never declared. If this is a global, consider adding a /** global: wc_stripe_admin_params */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
56
				} else {
57
					$( this ).css( 'border-color', '' );
58
					$( '.stripe-error-description', $( this ).parent() ).remove();
59
				}
60
			}).change();
61
62
			$( '#woocommerce_stripe_test_secret_key, #woocommerce_stripe_test_publishable_key' ).change( function() {
63
				var value = $( this ).val();
64
65
				if ( value.indexOf( '_live_' ) >= 0 ) {
66
					$( this ).css( 'border-color', 'red' ).after( '<span class="description stripe-error-description" style="color:red; display:block;">' + wc_stripe_admin_params.localized_messages.not_valid_test_key_msg + '</span>' );
0 ignored issues
show
Bug introduced by
The variable wc_stripe_admin_params seems to be never declared. If this is a global, consider adding a /** global: wc_stripe_admin_params */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
67
				} else {
68
					$( this ).css( 'border-color', '' );
69
					$( '.stripe-error-description', $( this ).parent() ).remove();
70
				}
71
			}).change();
72
73
			$( '#wc-gateway-stripe-apple-pay-domain' ).click( function( e ) {
74
				e.preventDefault();
75
76
				// Remove any previous messages.
77
				$( '.wc-stripe-apple-pay-domain-message' ).remove();
78
79
				var data = {
80
					'nonce': wc_stripe_admin_params.nonce.apple_pay_domain_nonce,
0 ignored issues
show
Bug introduced by
The variable wc_stripe_admin_params seems to be never declared. If this is a global, consider adding a /** global: wc_stripe_admin_params */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
81
					'action': 'wc_stripe_apple_pay_domain'
82
				};
83
84
				$.ajax({
85
					type:    'POST',
86
					data:    data,
87
					url:     wc_stripe_admin_params.ajaxurl,
88
					success: function( response ) {
89
						if ( true === response.success ) {
90
							$( '#wc-gateway-stripe-apple-pay-domain' ).html( wc_stripe_admin_params.localized_messages.re_verify_button_text ).after( '<p class="wc-stripe-apple-pay-domain-message" style="color:green;">' + response.message + '</p>' );
0 ignored issues
show
Bug introduced by
The variable wc_stripe_admin_params seems to be never declared. If this is a global, consider adding a /** global: wc_stripe_admin_params */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
91
92
							$( '.wc-gateway-stripe-apple-pay-domain-set' ).val( 1 );
93
94
						}
95
96
						if ( false === response.success ) {
97
							$( '#wc-gateway-stripe-apple-pay-domain' ).after( '<p class="wc-stripe-apple-pay-domain-message" style="color:red;">' + response.message + '</p>' );
98
99
							$( '.wc-gateway-stripe-apple-pay-domain-set' ).val( 0 );
100
						}
101
					}
102
				});
103
			});
104
		}
105
	};
106
107
	wc_stripe_admin.init();
108
});
109